declare var wx; 
class BeginScene extends eui.Component  {
    private _sound:egret.Sound;
    private _channel: egret.SoundChannel;
    private _pauseTime: number;
    private musicControl: eui.Image;
    private musicIsPlay: boolean = false;
    private toRankScene: eui.Image;
    private shareBtn: eui.Image;
    private gameStart: eui.Image;
    private bgm;
    
    public constructor() {
        super();
        this.skinName = 'startGame';
        this.init()
    }
    private init() {
        this.bgm = wx.createInnerAudioContext()
        this.bgm.autoplay = true
        this.bgm.loop = true
        this.bgm.src = 'audio/bgm.mp3';
        this.initEvents();

        wx.showShareMenu({
            success() {
                
            }
        })

        wx.onShareAppMessage(function () {
            // 用户点击了“转发”按钮
            return {
                title: '孩子从数学0基础秒杀大学生 就靠它了',      
                imageUrl: platform.globalPath + '/assets/img/share.jpg',    
                query:`take=${wx.getStorageSync('take')}`  
            }
        })
        wx.onShow(() => {
            this.bgm.play()
        })
    }

    private initEvents() {
        this.musicControl.addEventListener(egret.TouchEvent.TOUCH_TAP,  this.musicHandle, this);
        this.toRankScene.addEventListener(egret.TouchEvent.TOUCH_TAP,  this.tapHandler, this);
        this.shareBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,  this.share, this);
        this.gameStart.addEventListener(egret.TouchEvent.TOUCH_TAP,  this.game, this);
    }

    private game() {
		SceneMange.getInstance().changeScene('gameScene');
    }

    private share() {
        try {
            platform.request({
                url:platform.globalPath + '/button/record',
                method: 'POST',
                data: {userId: wx.getStorageSync('userId'),buttonType: 3}
            })
        } catch (error) {
            
        }
        wx.shareAppMessage({
            title:'孩子从数学0基础秒杀大学生 就靠它了',
            imageUrl: platform.globalPath + '/assets/img/share.jpg',    
            query:`take=${wx.getStorageSync('take')}`
        })
    }
    private musicHandle() {
        try {
            platform.request({
                url:platform.globalPath + '/button/record',
                method: 'POST',
                data: {userId: wx.getStorageSync('userId'),buttonType: 4}
            })
        } catch (error) {
            
        }

        if(platform.music){
            this.bgm.stop()
        }else {
            this.bgm.play()
        }
        platform.music = !platform.music;
        
    }
   	private tapHandler(){
        try {
            platform.request({
                url:platform.globalPath + '/button/record',
                method: 'POST',
                data: {userId: wx.getStorageSync('userId'),buttonType: 2}
            })
        } catch (error) {
            
        }
		SceneMange.getInstance().changeScene('rankScene');
	}
	//移除事件
	public release(){
		if(this.musicControl.hasEventListener(egret.TouchEvent.TOUCH_TAP)){
			this.musicControl.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.musicHandle,this);
		}

        if(this.toRankScene.hasEventListener(egret.TouchEvent.TOUCH_TAP)){
			this.toRankScene.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.tapHandler,this);
		}
	}
    
}